package com.syqc.utils

import com.syqc.comlib.Constants
import com.syqc.comlib.UserConstant
import com.syqc.comlib.utils.AppContext
import com.syqc.entity.VideoHost
import com.syqc.monitor.R
object UrlUtil {

    var project: String = "gps-web"

    private var http: String = "http://"

    //是否可以使用Https
    private var isHttps = false

    //设置
    fun setHttpOrHttps(isHttps: Boolean) {
        this.isHttps = isHttps
        http = if (isHttps) Constants.HTTPS
        else Constants.HTTP
    }

    fun getHttp() = http

    //获取当前是否是HTTPS
    fun getCurrentIsHttps() = isHttps

    private fun baseHttp() = "$http${UserConstant.current_ip}:${UserConstant.current_port}"


    fun baseHost() = "${baseHttp()}/$project"


    /** 获取指定平台得BaseAPI */
    fun newBase(ip: String, port: String) = "$http$ip:$port/$project/app/"

    //广告图
    fun getHomePicUrl(img: String) =
        "${baseHost()}/login?img=$img&cmpId=${UserConstant.current_cmpId}"


    //欢迎页
    fun getWelcomeUrl() = "${baseHost()}/login?img=app_load&cmpId=${UserConstant.current_cmpId}"

    //登录logo图
    fun getLoginLogoUrl() = "${baseHost()}/login?img=app_logo&cmpId=${UserConstant.current_cmpId}"


    //登录logo
    fun getLoginLogoUrl(logUrl: String) = "${baseHost()}$logUrl"

    //登录背景图
    fun getLoginBgUrl() = "${baseHost()}/login?img=app_logo_bg&cmpId=${UserConstant.current_cmpId}"

    //运输
    fun baseTransport() = "${baseHost()}/h5/mgr/"

    /** 扩展地址 */
    fun getLogoUrl() = "${baseHost()}/"


    private val videoToastMap = HashMap<String, String>()
    fun getToastMap(): HashMap<String, String> {
        if (videoToastMap.isEmpty()) {
            val videoToasts: Array<String> =
                AppContext.getArrayById(R.array.video_toast)
            videoToastMap["-9"] = videoToasts[30]
            videoToastMap["-8"] = videoToasts[31]
            videoToastMap["-7"] = videoToasts[32]
            videoToastMap["-6"] = videoToasts[0]
            videoToastMap["-5"] = videoToasts[33]
            videoToastMap["-4"] = videoToasts[1]
            videoToastMap["-3"] = videoToasts[2]
            videoToastMap["-2"] = videoToasts[3]
            videoToastMap["-1"] = videoToasts[4]
            videoToastMap["1"] = videoToasts[5]
            videoToastMap["2"] = videoToasts[6]
            videoToastMap["3"] = videoToasts[7]
            videoToastMap["10"] = videoToasts[8]
            videoToastMap["11"] = videoToasts[9]
            videoToastMap["12"] = videoToasts[10]
            videoToastMap["13"] = videoToasts[11]
            videoToastMap["14"] = videoToasts[12]
            videoToastMap["15"] = videoToasts[13]
            videoToastMap["16"] = videoToasts[14]
            videoToastMap["17"] = videoToasts[15]
            videoToastMap["20"] = videoToasts[16]
            videoToastMap["66"] = videoToasts[17]
            videoToastMap["77"] = videoToasts[18]
            videoToastMap["21"] = videoToasts[19]
            videoToastMap["30"] = videoToasts[20]
            videoToastMap["31"] = videoToasts[21]
            videoToastMap["32"] = videoToasts[22]
            videoToastMap["33"] = videoToasts[23]
            videoToastMap["34"] = videoToasts[24]
            videoToastMap["35"] = videoToasts[25]
            videoToastMap["44"] = videoToasts[26]
            videoToastMap["43"] = videoToasts[27]
            videoToastMap["88"] = videoToasts[28]
            videoToastMap["42"] = videoToasts[29]
        }
        return videoToastMap
    }

    private val cmdToastMap = HashMap<String, String>()
    fun getCmdToast(): HashMap<String, String> {
        if (cmdToastMap.isEmpty()) {
            val toasts: Array<String> =
                AppContext.getArrayById(R.array.cmd_state)
            cmdToastMap["-6"] = toasts[0]
            cmdToastMap["-5"] = toasts[1]
            cmdToastMap["-4"] = toasts[2]
            cmdToastMap["-3"] = toasts[3]
            cmdToastMap["-2"] = toasts[4]
            cmdToastMap["-1"] = toasts[5]
            cmdToastMap["0"] = toasts[6]
            cmdToastMap["1"] = toasts[7]
            cmdToastMap["2"] = toasts[8]
            cmdToastMap["3"] = toasts[9]
        }
        return cmdToastMap
    }

    /**********************终端视频FTP上传下载状态******************************************************/
    private val upDownMap = HashMap<String, String>()
    private fun getUpDownMap(): HashMap<String, String> {
        if (upDownMap.isEmpty()) {
            val videoToasts: Array<String> =
                AppContext.getArrayById(R.array.upload_download)
            upDownMap["-6"] = videoToasts[0]
            upDownMap["-4"] = videoToasts[1]
            upDownMap["-3"] = videoToasts[2]
            upDownMap["-2"] = videoToasts[3]
            upDownMap["-1"] = videoToasts[4]
            upDownMap["97"] = videoToasts[5]
            upDownMap["98"] = videoToasts[6]
            upDownMap["99"] = videoToasts[7]
        }
        return upDownMap
    }

    fun getLoadFtp(key: String): String {
        return getUpDownMap()[key] ?: ""
    }

    /** ************************************************************ */
    fun newBase() = "${baseHost()}/app/"

}

/**
 * 视频服务器处理类
 */
object VideoHostUtil {
    private val videoHosts = mutableListOf<VideoHost>()

    //全部集群数量
    private var hostSize: Int = 0

    /**
     * 处理视频服务集群
     */
    fun handHosts(videoHost: String, videoHosts: String?) {
        this.videoHosts.clear()
        if (videoHosts == null) {
            if (videoHost.isEmpty()) return
            this.videoHosts.add(getNewVideoHost(videoHost))
        } else {
            videoHosts.split(";").forEach { it ->
                this.videoHosts.add(getNewVideoHost(it))
            }
        }
        hostSize = this.videoHosts.size
    }

    //转换视频服务器端口
    private fun getNewVideoHost(videoHost: String): VideoHost {
        val hosts = videoHost.split(":")
        var port = hosts[1].toInt() - 1
        if (UserConstant.isHttps) {
            port += 10
        }
        return VideoHost(hosts[0], port)
    }

    //当前车辆对应的视频服务器集群下标
    fun getVideoHost(carId: String): VideoHost {
//        if (videoHosts.isEmpty()) return null
        val index = carId.toInt() % hostSize
        val host = videoHosts[index]
        host.index = index
        return host
    }

    //改变连接状态
    fun changeHost(index: Int, isClient: Boolean) {
        videoHosts[index].isClient = isClient
    }

    fun getHost(videoHost: VideoHost): String {
        return "${UrlUtil.getHttp()}${videoHost.ip}:${videoHost.port}"
    }

    //清除保存状态
    fun clearHostsClient() {
        videoHosts.forEach {
            it.isClient = null
        }
    }
}

object Keys {

    const val INTENT_CAR_VIDEOS: String = "Videos"

    //跳转相关
    const val INTENT_TITLE = "title"
    const val INTENT_DATA = "data"
    const val INTENT_OTHER = "other"
    const val INTENT_TYPE = "type"
    const val INTENT_CAR_ID = "carId"
    const val INTENT_ID = "id"
    const val INTENT_PLATE = "plate"
    const val INTENT_CAR_NAME = "carName"
    const val INTENT_TEAM_NAME = "teamName"
    const val INTENT_TEAM_ID = "teamId"
    const val INTENT_TEAM_NUM = "carNum"
    const val INTENT_ALARM_ID = "alarmId"
    const val INTENT_IS_VIDEO = "isVideo"
    const val INTENT_TIME = "startTime"
    const val INTENT_START_TIME = "start_time"
    const val INTENT_END_TIME = "end_time"
    const val INTENT_NO_AUTH = "no_auth"

    const val LOGIN_TYPE_IP = "ip"
    const val LOGIN_TYPE_PORT = "port"
    const val LOGIN_TYPE_HTTPS = "HTTPS"
    const val LOGIN_TYPE_USERID = "userId"
    const val LOGIN_TYPE_USER = "user"
    const val LOGIN_TYPE_LOGIN = "login_type"
    const val LOGIN_TYPE_COLOR_ID = "color_id"
    const val LOGIN_TYPE_PWD = "password"
    const val LOGIN_TYPE_CAR = "car"
    const val SETTING_ALARM = "setting_alarm"
    const val SETTING_ALARM_SHOW = "setting_alarm_show"
    const val SETTING_ARREARS = "setting_arrears"
    const val APP_CFG_HOME_SET = "app_cfg_home_set"
    const val SETTING_REFRESH_TIME = "app_arrears_auth"
    const val KEY_HOME_SET = "homeSet"

    //标志
    const val HOST_LOGO = "KEY"

    //公司ID
    const val CMP_ID = "cmpId"

    //视频相关
    const val VIDEO = "videos"

    //选中平台的下标
    const val CACHE_LOGIN_INDEX = "login_index"

    //所有平台数据
    const val CACHE_LOGIN_USERS = "login_users"

}